export default {
  data () {
    return {
      noiseEchart: null,
      noiseDataMap: new Map(),
      noiseOption: {
        title: {
          text: '近7天噪音变化情况(单位：dB)',
          textStyle: {
            color: '#363c3c',
            fontSize: 14,
            fontWeight: 'normal',
            fontFamily: 'Microsoft YaHei'
          }
        },
        tooltip: {
          trigger: 'axis',
          confine: true,
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          right: 0,
          itemWidth: 25,
          itemHeight: 2,
          padding: [7, 0, 0, 0],
          textStyle: {
            color: '#898989'
          },
          data: [{name: 'TSP', icon: 'rect'}, {name: 'PM10', icon: 'rect'}, {name: 'PM2.5', icon: 'rect'}]
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 0,
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['1日', '2日', '3日', '4日', '5日', '昨天', '今日'],
            axisLabel: {
              color: '#939393'
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#e8e8e8',
                width: 2
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#a8c5ed'
              }
            }
          }
        ],
        yAxis: [
          {
            show: false,
            type: 'value',
            name: '',
            nameTextStyle: {
              color: '#a8c5ed'
            },
            axisLabel: {
              show: false,
              color: '#a8c5ed'
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#214a60',
                width: 2
              }
            },
            splitLine: {
              lineStyle: {
                type: 'dashed',
                color: '#e8e8e8'
              }
            },
            position: 'right'
          }
        ],
        series: [{
          barWidth: 15,
          label: {
            show: true,
            color: '#898989',
            position: 'top'
          },
          itemStyle: {
            color: '#33cadb'
          },
          data: [],
          type: 'bar'
        }]
      }
    }
  },
  methods: {
    initNoise () {
      this.initNoiseTime()
      this.initNoiseData()
      this.noiseOption.series[0].data = [...this.noiseDataMap.values()].map(item => {
        let color = '#33cadb'
        if (item > Number.parseInt(this.groupParams.NOISEWARN)) {
          color = '#fda730'
        }
        if (item > Number.parseInt(this.groupParams.NOISEALARM)) {
          color = '#fa5655'
        }
        return {
          value: item,
          itemStyle: {
            color: color
          }
        }
      })
      this.noiseEchart = this.$Echarts.init(this.$refs.noise)
      this.noiseEchart.setOption(this.noiseOption)
    },
    initNoiseTime () {
      this.noiseOption.xAxis[0].data = []
      this.noiseDataMap.clear()
      for (let i = 0; i < 7; i++) {
        let DD = this.$moment().subtract(i, 'days').format('DD')

        this.noiseDataMap.set(this.$moment().subtract(6, 'days').add(i, 'days').format('YYYY-MM-DD'), 0)
        if (i === 0) {
          this.noiseOption.xAxis[0].data.unshift(`今日`)
        } else if (i === 1) {
          this.noiseOption.xAxis[0].data.unshift(`昨天`)
        } else {
          this.noiseOption.xAxis[0].data.unshift(`${DD}日`)
        }
      }
    },
    initNoiseData () {
      this.dustWorksumbydayVOS.forEach((item) => {
        let time = this.$moment(item.updateTime * 1000).format('YYYY-MM-DD')
        this.noiseDataMap.set(time, item.noiseAvg)
      })
    }
  }
}
